package com.xsg.printer.core.conditions;

import com.xsg.printer.core.enums.ColorEnum;
import com.xsg.printer.core.enums.FloatAlignEnum;
import com.xsg.printer.core.model.*;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class TemplateStepConfig {

    public static GlobalConfig builder() {
        return new TemplateStepBuilder();
    }

    public interface GlobalConfig {

        DivConfig body(int width, int height, int bgColor, XsgHead head, XsgBorder border);

        default DivConfig body(int width, int height, XsgBorder border) {
            return body(width, height, ColorEnum.WHITE.getType(), null, border);
        }

        default DivConfig body(int width, int height) {
            return body(width, height, ColorEnum.WHITE.getType(), null, XsgBorder.hiddenBorder());
        }
    }

    public interface DivConfig {

        default DivConfig div(String id, int width, int height, FloatAlignEnum floatAlign, XsgBorder border, ElementConfig elementConfig) {
            return div(id, width, height, floatAlign, border, elementConfig, null);
        }

        default DivConfig div(String id, int width, int height, FloatAlignEnum floatAlign, XsgBorder border, Consumer<DivConfig> configConsumer) {
            return div(id, width, height, floatAlign, border, null, configConsumer);
        }

        default DivConfig div(String id, int width, int height, ElementConfig elementConfig) {
            return div(id, width, height, FloatAlignEnum.LEFT, XsgBorder.hiddenBorder(), elementConfig);
        }

        default DivConfig div(int width, int height, FloatAlignEnum floatAlign) {
            return div(null, width, height, floatAlign, XsgBorder.hiddenBorder(), null, null);
        }

        default DivConfig div(int width, int height, FloatAlignEnum floatAlign, Consumer<DivConfig> configConsumer) {
            return div(null, width, height, floatAlign, XsgBorder.hiddenBorder(), null, configConsumer);
        }

        default DivConfig div(int width, int height, FloatAlignEnum floatAlign, XsgBorder border, ElementConfig elementConfig) {
            return div(null, width, height, floatAlign, border, elementConfig, null);
        }

        default DivConfig div(int width, int height, FloatAlignEnum floatAlign, ElementConfig elementConfig) {
            return div(null, width, height, floatAlign, XsgBorder.hiddenBorder(), elementConfig, null);
        }

        DivConfig div(String id, int width, int height, FloatAlignEnum floatAlign, XsgBorder border, ElementConfig elementConfig, Consumer<DivConfig> configConsumer);

        XsgTemplate build();
    }

    @Data
    public static class TemplateStepBuilder implements GlobalConfig, DivConfig{

        public XsgTemplate template;

        private List<XsgDiv> divs;

        public TemplateStepBuilder() {
            this.template = new XsgTemplate();
            this.divs = new ArrayList<>();
        }

        @Override
        public DivConfig body(int width, int height, int bgColor, XsgHead head, XsgBorder border) {
            template.setWidth(width).setHeight(height).setColor(bgColor).setHead(head).setBorder(border);
            return this;
        }

        @Override
        public DivConfig div(String id, int width, int height, FloatAlignEnum floatAlign, XsgBorder border, ElementConfig elementConfig, Consumer<DivConfig> configConsumer) {
            XsgDiv xsgDiv = new XsgDiv().setId(id).setWidth(width).setHeight(height).setFloatAlign(floatAlign.getCode()).setBorder(border);
            xsgDiv.setElements(elementConfig != null ? elementConfig.get() : null);
            if (configConsumer != null) {
                TemplateStepBuilder divConfig = new TemplateStepBuilder();
                configConsumer.accept(divConfig);
                xsgDiv.setDivs(divConfig.getDivs());
            }
            divs.add(xsgDiv);
            return this;
        }

        public XsgTemplate build() {
            template.setDivs(divs);
            return template;
        }
    }
}
